// pages/addTable/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists:[],
    id:'',
    name:'',
    userNum:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      id:options.id
    })
    this.storeDeskDetails()
  },
  inputs(e){
    console.log(e)
    this.setData({
      userNum:e.detail.value
    })
  },
  input2(e){
    console.log(e)
    this.setData({
      name:e.detail.value
    })
  },
  storeDeskDetails(){
    let data = {
      id:this.data.id
    }
    app.agriknow.storeDeskDetails(data).then(res=>{
      console.log(res)
      var productIds = []
      var list=res.data.products
     
      this.setData({
        lists:list,
        userNum:res.data.userNum,
        name:res.data.name
      })
      
    })
  },
  
  gotoAddcp(){
    wx.navigateTo({
      url: '/pages/uploadCp/index',
    })
  },

  gotoDatileCp(){
    wx.navigateTo({
      url: '/pages/datileCp/index?id='+this.data.id,
    })
  },
  unique (arr) {
    return Array.from(new Set(arr))
  },
  save(){
    wx.showLoading({
      title: '上传中',
      mask:true
    })
    let productIds = []
    this.data.lists.forEach((item,index)=>{
      productIds.push(item.id)
    })

    if(!this.data.userNum) return this.tip('请输入就餐人数');
    if(!this.data.name) return this.tip('请输入桌子号码');
    if(this.data.lists.length==0) return this.tip('请选择产品');
    
    let data = {
      name:this.data.name,
      userNum:this.data.userNum,
      storeId:wx.getStorageSync('userInfo').storeId,
      productIds:productIds,
      id:this.data.id
    }
    app.agriknow.storeUpdDesk(data).then(res=>{
      console.log(res)
      wx.hideLoading()
      if(res.code=='000000'){
        this.tipsMessage('修改成功')
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000);
      }else{
        this.tipsMessage(res.data)
      }
    }).catch(res=>{
      wx.hideLoading()
      wx.showToast({
        title: res.message,
        icon: 'none',
        duration: 2000,
        mask: true
      })
    })
  },
  //提示信息
  tipsMessage(text) {
    wx.showToast({
      title: text,
      icon: 'none',
      duration: 2000,
      mask: true
    })
  },
  tip(name){
    wx.showToast({
      title: name,
      icon:'none',
      duration: 2000
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  delete(e){
    let list = this.data.lists
    let index = e.currentTarget.dataset.index
    list.splice(index,1)
    this.setData({
      lists:list
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})